package com.ruoyi.web.controller.self;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.self.domain.RmTestrecord;
import com.ruoyi.self.service.IRmTestrecordService;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.IRmUserDeptService;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 考试记录Controller
 * 
 * @author ruoyi
 * @date 2019-11-30
 */
@Controller
@RequestMapping("/self/testrecord")
public class RmTestrecordController extends BaseController
{
    private String prefix = "self/testrecord";

    @Autowired
    private IRmTestrecordService rmTestrecordService;

    @Autowired
    private IRmUserDeptService rmUserDeptService;

    @Autowired
    private ISysDeptService deptService;

    @RequiresPermissions("self:testrecord:view")
    @GetMapping()
    public String testrecord(ModelMap mmap)
    {
        Subject subject = SecurityUtils.getSubject();
        SysUser sysUser = (SysUser) subject.getPrincipal();
        List<SysDept> sysDepts;
        if(subject.hasRole("instructor")){
            sysDepts = rmUserDeptService.selectDeptsSelectByUserId(sysUser.getUserId());
            mmap.put("userId",sysUser.getUserId());
        } else {
            SysDept sysDept = new SysDept();
            sysDept.setParentId((long) 110);
            sysDepts = deptService.selectDeptList(sysDept);
            mmap.put("userId","");
        }
        mmap.put("mageDepts",sysDepts);
        return prefix + "/testrecord";
    }

    /**
     * 查询考试记录列表
     */
    @RequiresPermissions("self:testrecord:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RmTestrecord rmTestrecord)
    {
        System.out.println("start select1 !");
        startPage();
        System.out.println("start select2 !");
        List<RmTestrecord> list = rmTestrecordService.selectRmTestrecordList(rmTestrecord);
        System.out.println("end select !");
        //System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 查询考试记录列表
     */
    /*@RequiresPermissions("self:testrecord:list")*/
    @PostMapping("/list2")
    @ResponseBody
    public List<RmTestrecord> list2(RmTestrecord rmTestrecord)
    {
        List<RmTestrecord> list = rmTestrecordService.selectRmTestrecordList2(rmTestrecord);
        return list;
    }

    /**
     * 导出考试记录列表
     */
    @RequiresPermissions("self:testrecord:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RmTestrecord rmTestrecord)
    {
        List<RmTestrecord> list = rmTestrecordService.selectRmTestrecordList(rmTestrecord);
        ExcelUtil<RmTestrecord> util = new ExcelUtil<RmTestrecord>(RmTestrecord.class);
        return util.exportExcel(list, "testrecord");
    }

    /**
     * 新增考试记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存考试记录
     */
    @RequiresPermissions("self:testrecord:add")
    @Log(title = "考试记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RmTestrecord rmTestrecord)
    {
        return toAjax(rmTestrecordService.insertRmTestrecord(rmTestrecord));
    }

    /**
     * 修改考试记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        RmTestrecord rmTestrecord = rmTestrecordService.selectRmTestrecordById(id);
        mmap.put("rmTestrecord", rmTestrecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存考试记录
     */
    @RequiresPermissions("self:testrecord:edit")
    @Log(title = "考试记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RmTestrecord rmTestrecord)
    {
        return toAjax(rmTestrecordService.updateRmTestrecord(rmTestrecord));
    }

    /**
     * 删除考试记录
     */
    @RequiresPermissions("self:testrecord:remove")
    @Log(title = "考试记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(rmTestrecordService.deleteRmTestrecordByIds(ids));
    }
}
